#coding:utf-8
from BeautifulSoup import BeautifulSoup
import urllib2,os
import hashlib,sqlite3,datetime
  
def init_apps():
    #req = urllib2.Request("http://store.ovi.com.cn/?lid=storeherobtnnli&cid=ovistore-fw-ilc-herobtnnli-acq-na-ovicom-g0-na-1")   
    #page = urllib2.urlopen(req) 
    #print u"数据初始开始"
    page = open("ovi.html").read()
    bf = BeautifulSoup(page)
    bf.prettify("utf-8")
    softList =  bf.findAll("li",{"class":"entity nth-child-odd "})
    apps = []
    idd = 30
    for soft in softList:
        idd += 1
        thumb_a =  soft.find("a" ,{"class":"thumb"})
        thumb_img = thumb_a.find("img")['src']
        title = soft.find("h4" ).find("a").string
        typ =  soft.find("span" ).string
        auther_el = soft.find("p" ,{"class":"beforeRating"}).find("a")
        auther = auther_el.string
        auther_url = auther_el['href']
        summary = soft.find("p" ,{"class":"summary"}).string
    
        image = "upload/nokia/nokia-app-%s.png"%idd

        data = urllib2.urlopen(thumb_img).read()
        imgfile = open(image,"wb")
        imgfile.write(data)
        imgfile.close()
        
        conn = sqlite3.connect("../mobeir_db")
        conn.isolation_level = None
        
        #conn.execute("inert into mobeir_apptag values(%s)"%(typ))
        
        conn.execute("""insert into mobeir_app 
            (user_id,name,version,app_url,auther,auther_url,
            summary,thumb,audit,score,group_id,last_update)
            values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')
        """%(1,title,"N/A",thumb_a['href'],auther,auther_url,summary,image,True,0,1,datetime.datetime.now()))
        
init_apps()


    

    


